/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SearchFrame.java
 *
 * Created on Apr 29, 2011, 8:10:46 PM
 */

package courseScheduler;

import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author david
 */
public class SearchFrame extends javax.swing.JFrame {

    /** Creates new form SearchFrame */
    public SearchFrame(String [] newArray, String [] newValues, java.sql.Connection newConn) {
        Array = newArray;
        Values = newValues;
        conn = newConn;
        String [] columns = {"First Name","Last Name","Email"};
        createQuery();
        System.out.println(sql);
        try {
            rs = conn.createStatement().executeQuery(sql);
            rs.last();
            rows = rs.getRow();
            results = new String [rows][3];
            rs.beforeFirst();
            while (rs.next()){
                results[rs.getRow()-1][0] = rs.getString(1);
                results[rs.getRow()-1][1] = rs.getString(2);
                results[rs.getRow()-1][2] = rs.getString(3);
                System.out.println(results[rs.getRow()-1][0] + " " + results[rs.getRow()-1][1] + " " + results[rs.getRow()-1][2]);
            }
            PersonTable.setModel(new javax.swing.table.DefaultTableModel(results, columns));
            this.repaint();
        } catch (SQLException ex) {
            Logger.getLogger(SearchFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        PersonTable = new javax.swing.JTable();
        SelectButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        PersonTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "First Name", "Last Name", "Email"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(PersonTable);

        SelectButton.setText("Select");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 353, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(304, Short.MAX_VALUE)
                        .addComponent(SelectButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(SelectButton)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
    */
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable PersonTable;
    private javax.swing.JButton SelectButton;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

    String [] Array,Values;
    
    java.sql.Connection conn;
    java.sql.ResultSet rs;

    String [][] results;
    String sql;

    int rows;
    
    private void createQuery(){
        sql = "SELECT f_name,l_name,e_mail FROM person";
        if(Array[0].equals("m_initial")){
            sql += " WHERE " + Array[0] + " = \'" + Values[0].charAt(0) + "\'";
        }
        else {
            sql += " WHERE " + Array[0] + " LIKE \"" + Values[0] + "\"";
        }
        for (int i = 1; i < Array.length; i ++){
            if(Array[i].equals("m_initial")){
                sql += " AND " + Array[i] + " = \'" + Values [i].charAt(0) + "\'";
            }
            else{
                sql += " AND " + Array[i] + " LIKE \"" + Values[i] + "\"";
            }
        }
        sql += ";";
    }
}